package com.chenfan.controller;

import com.chenfan.pojo.PageBean;
import com.chenfan.pojo.Result;
import com.chenfan.pojo.Sum;
import com.chenfan.pojo.User;
import com.chenfan.service.SumService;
import com.chenfan.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/users")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private SumService sumService;
    /**
     * 查询用户
     * @return
     */
    @GetMapping
    public Result list(){
        log.info("查询所有用户");
        List<User> userList=userService.list();
        return Result.success(userList);
    }

    /**
     * 借书
     */
    @PostMapping("/j/{id}")
    public Result add(@PathVariable Integer id){
        log.info("借阅某个id的图书{}",id);
       sumService.add(id);
        return Result.success();
    }
    /**
     * 还书
     */
    @PostMapping("/h/{id}")
    public Result take(@PathVariable Integer id){
        log.info("还某个id的图书{}",id);
        sumService.take(id);
        return Result.success();
    }
    /*@GetMapping("/sum")
    public Result list1() {
        log.info("查询最多的书");
        List<Sum> total=sumService.list();
        return Result.success();
    }*/
    @GetMapping("/sum")
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "5") Integer pageSize){
        log.info("分页查询，参数：{}，{}",page,pageSize);
        PageBean pageBean=sumService.page(page,pageSize);
        return Result.success(pageBean);
    }
}
